/**   
* @Package com.app.open.common.tree 
*/
package com.rework.joss.ztree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.rework.joss.ztree.util.Assert;

/**
 * 
 * @ClassName. BaseStaticTreeNode
 * @Description. 静态树节点
 * @author zhangyang
 * @date 2015年8月8日 上午8:45:05
 * @version V1.0
 */
public class BaseStaticTreeNode implements TreeNode{
	
	private static final Log logger = LogFactory.getLog(BaseStaticTreeNode.class);

	/**
	 * 节点id
	 */
	private String id;
	
	/**
	 * 节点名称
	 */
	private String name;
	
	/**
	 * 父节点id
	 */
	private String fatherId;
	
	/**
	 * 记录 treeNode 节点是否为父节点。
	 */
	private Boolean isParent;
	
	/**
	 * 记录 treeNode 节点的 展开 / 折叠 状态。
	 */
	private boolean open = false;
	
	/**
	 * 设置点击节点后在何处打开 url
	 */
	private String target;
	
	/**
	 * 节点链接的目标 URL
	 */
	private String url;
	
	/**
	 * 自定义-节点类型
	 */
	private String type;
	
	/**
	 * 自定义-节点状态
	 */
	private String state;
	
	/**
	 * 节点的 checkBox / radio 的 勾选状态
	 */
	private boolean checked = false;
	
	/**
	 * 设置节点的 checkbox / radio 是否禁用
	 */
	private boolean chkDisabled = false;
	
	/**
	 * 强制节点的 checkBox / radio 的 半勾选状态
	 */
	private boolean halfCheck = false;
	
	/**
	 * 自定义-其他
	 */
	private Map<String,Object> attributs;
	
	private List<TreeNode> children = null;
	
	public BaseStaticTreeNode() {}

	
	public static class Builder{
		private String id;
		private String name;
		private String fatherId;
		private Boolean isParent;
		private boolean open = false;
		private String target;
		private String url;
		private String type;
		private String state;
		private boolean checked = false;
		private boolean chkDisabled = false;
		private boolean halfCheck = false;
		private Map<String,Object> attributs;
		
		public Builder(String id, String name,String fatherId) {
			this.id = id;
			this.name = name;
			this.fatherId = fatherId;
		}
		
		public Builder isParent(boolean val) {
			this.isParent = val;
			return this;
		}
		
		public Builder open(boolean val) {
			this.open = val;
			return this;
		}
		
		public Builder target(String val) {
			this.target = val;
			return this;
		}
		
		public Builder url(String val) {
			this.url = val;
			return this;
		}
		
		public Builder type(String val) {
			this.type = val;
			return this;
		}
		
		public Builder state(String val) {
			this.state = val;
			return this;
		}
		
		public Builder checked(boolean val) {
			this.checked = val;
			return this;
		}
		
		public Builder chkDisabled(boolean val) {
			this.chkDisabled = val;
			return this;
		}
		
		public Builder halfCheck(boolean val) {
			this.halfCheck = val;
			return this;
		}
		
		public Builder attributs(Map<String,Object> val) {
			this.attributs = val;
			return this;
		}
		
		public BaseStaticTreeNode build(){
		    return new BaseStaticTreeNode(this);
		}
		
	}
	
	private BaseStaticTreeNode(Builder builder){
		this.id = builder.id;
		this.name = builder.name;
		this.fatherId = builder.fatherId;
		this.isParent = builder.isParent;
		this.open = builder.open;
		this.target = builder.target;
		this.url = builder.url;
		this.type = builder.type;
		this.state = builder.state;
		this.checked = builder.checked;
		this.chkDisabled = builder.chkDisabled;
		this.halfCheck = builder.halfCheck;
		this.attributs = builder.attributs;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}


	public String getFatherId() {
		return fatherId;
	}

	public void setFatherId(String fatherId) {
		this.fatherId = fatherId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	
	

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Map<String, Object> getAttributs() {
		return attributs;
	}

	public void setAttributs(Map<String, Object> attributs) {
		this.attributs = attributs;
	}
	
	public void attr(String key,Object value){
		if(this.attributs == null){
			this.attributs = new HashMap<String,Object>();
		}
		this.attributs.put(key, value);
	}
	
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Boolean getIsParent() {
		if(isParent !=null) {
			return isParent;
		}else if(children == null || children.isEmpty()){
			return false;
		}
		return true;
	}

	public void setIsParent(Boolean isParent) {
		this.isParent = isParent;
	}

	public boolean isOpen() {
		return open;
	}

	public void setOpen(boolean open) {
		this.open = open;
	}

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		this.checked = checked;
	}

	public boolean isChkDisabled() {
		return chkDisabled;
	}

	public void setChkDisabled(boolean chkDisabled) {
		this.chkDisabled = chkDisabled;
	}

	public boolean isHalfCheck() {
		return halfCheck;
	}

	public void setHalfCheck(boolean halfCheck) {
		this.halfCheck = halfCheck;
	}

	public void addChild(BaseStaticTreeNode currentNode) {
		if(this.children == null) {
			this.children = new ArrayList<TreeNode>();
		}
		children.add(currentNode);
	}

	public void addChilds(List<TreeNode> currentNodes) {
		if(this.children == null) {
			this.children = new ArrayList<TreeNode>();
		}
		children.addAll(currentNodes);
	}
	
	public List<TreeNode> getChildren() {
		return children;
	}

	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}

	
	@Override
	public boolean equals(Object obj){
		if(obj instanceof BaseStaticTreeNode){
			BaseStaticTreeNode node  = (BaseStaticTreeNode) obj;
			Assert.notNull(this.getId(),"id为空不能做为一个有效的节点");
			if(this.getId().equals(node.getId())){
				return true;
			}
		}
		return super.equals(obj);
	}
	
	@Override
	public int hashCode() {
		return super.hashCode();
	}
	
	@Override
	protected Object  clone() {
		Object target = null;
		try{
			target = BeanUtils.cloneBean(this);
		}catch(Exception e){
			logger.error("树节点复制出错",e);
		}
		return target;
	}


	
	
	
}
